[F#] Parallelize code in nested loops
Posted
by Juliet
on Stack Overflow
See other posts from Stack Overflow
or by Juliet
Published on 2009-01-05T03:52:55Z
Indexed on
2010/05/10
23:44 UTC
Read the original article
Hit count: 300
You always hear that functional code is inherently easier to parallelize than non-functional code, so I decided to write a function which does the following:
Given a input of strings, total up the number of unique characters for each string. So, given the input [ "aaaaa"; "bbb"; "ccccccc"; "abbbc" ]
, our method will returns a: 6; b: 6; c: 8
.
Here's what I've written:
(* seq<#seq<char>> -> Map<char,int> *)
let wordFrequency input =
input
|> Seq.fold (fun acc text ->
(* This inner loop can be processed on its own thread *)
text
|> Seq.choose (fun char -> if Char.IsLetter char then Some(char) else None)
|> Seq.fold (fun (acc : Map<_,_>) item ->
match acc.TryFind(item) with
| Some(count) -> acc.Add(item, count + 1)
| None -> acc.Add(item, 1))
acc
) Map.empty
This code is ideally parallelizable, because each string in input
can be processed on its own thread. Its not as straightforward as it looks since the innerloop adds items to a Map shared between all of the inputs.
I'd like the inner loop factored out into its own thread, and I don't want to use any mutable state. How would I re-write this function using an Async workflow?
© Stack Overflow or respective owner